﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Data;
using System.Text;
using JumbotCms.Common;
using JumbotCms.DataObject;
namespace JumbotCms.API
{
    public class FrontHtml : BasicPage
    {
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ReturnErrorIP();
            if (!site.IsOpen)
            {
                Response.Write(site.CloseMessage);
                Response.End();
            }
            if (site.IsTiming && !SysOpration(site.OpenTime))
            {
                Response.Write(site.CloseMessage);
                Response.End();
            }
        }

        public bool SysOpration(string sysObj)
        {
            string[] t2 = sysObj.Split('|');
            int t1 = t2.Length;
            int time1 = DateTime.Now.Hour;
            if (t1 % 2 != 0)
            {
                return false;
            }
            for (int i = 0; i < t1; i += 2)
            {
                if (time1 < Convert.ToInt32(t2[i].ToString()) || time1 > Convert.ToInt32(t2[i + 1].ToString())) return false;
            }
            return true;
        }
        public bool CheckCookiesCode()
        {
            string _code = q("code");
            //if (Request.Cookies["ValidateCode"] == null)
            //    return false;
            //if (Request.Cookies["ValidateCode"].Value != _code)
            if (Jumbot.Common.Cookie.GetValue("ValidateCode") != _code)
                return false;
            return true;
        }
        /// <summary>
        /// 解析主站的基本信息
        /// </summary>
        /// <param name="PageStr"></param>
        protected void ReplaceSiteTags(ref string PageStr)
        {
            TemplateEngine te = new TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.ReplaceSiteTags(ref PageStr);
        }
        /// <summary>
        /// 获得页面html
        /// </summary>
        /// <param name="_page"></param>
        /// <returns></returns>
        protected string GetPageHtml(string _page)
        {
            if (!Jumbot.Common.DirFile.FileExists(site.Dir + "templates/" + _page + ".htm"))
                _page = "user_login";
            string PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/" + _page + ".htm");
            TemplateEngine te = new TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.ReplacePublicTag(ref PageStr, "0", te.DefaultSkinId);
            return PageStr;
        }
        protected string GetContentFile(string _channelID, string _channelType, string _contentID, int _currentPage)
        {
            IModule module = (IModule)Activator.CreateInstance(Type.GetType(String.Format("JumbotCms.API.M{0}", _channelType), true, true));
            return module.GetContent(doh, _channelID, _contentID, _currentPage);
        }
        /// <summary>
        /// 生成频道内容页
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ContentList"></param>
        /// <param name="i"></param>
        public void GetChannel_Content(TemplateEngine te, DataTable dt, ref string PageStr, ref System.Collections.ArrayList ContentList, int i)
        {
            doh.Reset();
            doh.ConditionExpress = "IsOut=0 AND id=" + dt.Rows[i]["ClassId"].ToString();
            string TempId = doh.GetValue("jcms_normal_class", "ContentTemp").ToString();
            string ClassName = doh.GetValue("jcms_normal_class", "Title").ToString();
            string pId = string.Empty;
            string sId = string.Empty;
            doh.Reset();
            doh.SqlCmd = "SELECT TOP 1 [pid],[Source],[SkinId] FROM [jcms_normal_template] WHERE [Id]=" + TempId;
            DataTable dtTemplate = doh.GetDataTable();
            if (dtTemplate.Rows.Count > 0)
            {
                pId = dtTemplate.Rows[0]["pid"].ToString();
                sId = dtTemplate.Rows[0]["SkinId"].ToString();
                TemplateProject tp = new TemplateProject();
                PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/" + tp.GetDir(pId, doh) + "/" + dtTemplate.Rows[0]["Source"].ToString());
            }
            dtTemplate.Clear();
            dtTemplate.Dispose();
            te.PageNav = "<script type=\"text/javascript\" src=\"" + site.Dir + te.Channel.Dir + "/js/classnav_" + dt.Rows[i]["ClassId"].ToString() + ".js\"></script>&nbsp;&gt;&nbsp;内容";
            te.PageTitle = dt.Rows[i]["Title"] + "_" + te.Channel.Title + "_" + site.Name + site.TitleTail;
            //te.PageKeywords = site.Keywords;
            //试试新方法，看看性能消耗
            te.PageKeywords =  Jumbot.Common.WordSpliter.GetKeyword(dt.Rows[i]["Title"].ToString(), "~/_data/default.dic") + "," + site.Keywords;
            te.PageDescription = Jumbot.Common.Strings.SimpleLineSummary(dt.Rows[i]["Summary"].ToString());
            te.ReplacePublicTag(ref PageStr, pId, sId);
            te.ReplaceChannelTag(ref PageStr, dt.Rows[i]["ChannelId"].ToString());
            te.ReplaceClassTag(ref PageStr, dt.Rows[i]["ClassId"].ToString());
            te.ReplaceRepeatTag(ref PageStr);
        }
        /// <summary>
        /// 判断插件是否已经启用
        /// </summary>
        /// <param name="ExtendName">插件名称</param>
        public void CheckExtendState(string ExtendName)
        {
            doh.Reset();
            doh.ConditionExpress = "name=@name and Enabled=1";
            doh.AddConditionParameter("@name", ExtendName);
            if (!doh.Exist("jcms_normal_extends"))
            {
                Response.Write("插件未启动");
                Response.End();
            }
        }

    }
}